class ScaleAndRotate extends Laya.Script {
    protected lastMouseX: number;
    protected lastMouseY: number;
    protected resultRotation = new Laya.Quaternion();
    protected isMouseDown: Boolean;
    protected rotaionSpeed: number = 0.005;//旋转速度;
    protected scaleSpeed: number = 0.001; //缩放因子

    protected offsetXReal: number = 0;
    protected offsetYReal : number = 0;
    protected alpha : number = 0.8;
    private drapAlpha:number = 0.8;

    private touches:Array<any>;
    private lastDistance: number = 0;

    protected scene: Laya.Scene;
    protected target: Laya.MeshSprite3D;

    constructor() {
        super();
    }


    public _initialize(owner: Laya.Sprite3D): void {
        super._initialize(owner);
        Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.mouseDown);
        Laya.stage.on(Laya.Event.MOUSE_UP, this, this.mouseUp);
        Laya.stage.on(Laya.Event.MOUSE_OUT, this, this.mouseOut);
        Laya.stage.on(Laya.Event.MOUSE_WHEEL,this,this.mouseWheel);

        this.target = owner as Laya.MeshSprite3D;
    }

    public _update(state: Laya.RenderState): void {
        super._update(state);
        this.updateTarget(state.elapsedTime);

        this.OffsetJugd();
        if(!this.isMouseDown)
        {
            this.offsetXReal *= this.drapAlpha;
            this.offsetYReal *= this.drapAlpha;
        }
    }

    protected mouseWheel(e: Laya.Event):void
    {
        // var distance: number = this.getDistance(e.touches);
        var dis:number = e.delta;
        //判断当前距离与上次距离变化，确定是放大还是缩小
        var factor: number = dis * this.scaleSpeed * 10;

        var localScale = this.target.transform.localScale;
        var scale = new Laya.Vector3(localScale.x + factor,
                                        localScale.y + factor,
                                        localScale.z + factor);
        if(scale.x > 0.1 && scale.y > 0.1 && scale.z > 0.1)
        {
            this.target.transform.localScale = scale;
        }

    }

    protected mouseDown(e: Laya.Event): void
    {
        this.lastMouseX = Laya.stage.mouseX;
        this.lastMouseY = Laya.stage.mouseY;
        this.isMouseDown = true;

        this.touches = e.touches;
        if (this.touches && this.touches.length == 2) {
            this.lastDistance = this.getDistance(this.touches);
            Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
        }
    }

    protected mouseUp(e: Laya.Event): void {
        this.isMouseDown = false;
        this.touches = e.touches;
        Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
    }

    protected mouseOut(e: Laya.Event): void {
        this.isMouseDown = false;
        this.touches = e.touches;
        Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
    }

    protected mouseMove(e:Laya.Event):void
    {
        var distance: number = this.getDistance(e.touches);

        //判断当前距离与上次距离变化，确定是放大还是缩小
        var factor: number = (distance - this.lastDistance) * this.scaleSpeed;

        var localScale = this.target.transform.localScale;
        var scale = new Laya.Vector3(localScale.x + factor,
                                        localScale.y + factor,
                                        localScale.z + factor);
        if(scale.x > 0.1 && scale.y > 0.1 && scale.z > 0.1)
        {
            this.target.transform.localScale = scale;
        }

        this.lastDistance = distance;
    }
    private getDistance(points: Array<any>): number
    {
        var distance: number = 0;
        if (points && points.length == 2)
        {
            var dx: number = points[0].stageX - points[1].stageX;
            var dy: number = points[0].stageY - points[1].stageY;

            distance = Math.sqrt(dx * dx + dy * dy);
        }
        return distance;
    }

    protected updateTarget(elapsedTime: number): void
    {
        if (this.isMouseDown && this.touches && this.touches.length == 1) {
            var offsetX = Laya.stage.mouseX - this.lastMouseX;
            var offsetY = Laya.stage.mouseY - this.lastMouseY;

            this.offsetXReal = this.offsetXReal * this.alpha + (1 - this.alpha) * offsetX;
            this.offsetYReal = this.offsetYReal * this.alpha + (1 - this.alpha) * offsetY;

            this.OffsetJugd();
            // if( Math.abs(this.offsetXReal) <= 0.01 ){
            //     this.offsetXReal = 0;
            //     this.offsetYReal = 0;
            // }
            // offsetX = this.offsetXReal;
            // offsetY = this.offsetYReal;

            // this.target.transform.rotate(new Laya.Vector3(0,offsetX * this.rotaionSpeed * elapsedTime,0) ,false,false);
            // this.target.transform.rotate(new Laya.Vector3(offsetY * this.rotaionSpeed * elapsedTime,0,0) ,false,false);


        }else if(this.isMouseDown && !this.touches){
            var offsetX = Laya.stage.mouseX - this.lastMouseX;
            var offsetY = Laya.stage.mouseY - this.lastMouseY;

            this.offsetXReal = this.offsetXReal * this.alpha + (1 - this.alpha) * offsetX;
            this.offsetYReal = this.offsetYReal * this.alpha + (1 - this.alpha) * offsetY;

            this.OffsetJugd();
            // if( Math.abs(this.offsetXReal) <= 0.01 ){
            //     this.offsetXReal = 0;
            //     this.offsetYReal = 0;
            // }
            // offsetX = this.offsetXReal;
            // offsetY = this.offsetYReal;

            // this.target.transform.rotate(new Laya.Vector3(0,offsetX * this.rotaionSpeed * elapsedTime,0) ,false,false);
            // this.target.transform.rotate(new Laya.Vector3(offsetY * this.rotaionSpeed * elapsedTime,0,0) ,false,false);
        }

        if(this.isMouseDown && this.touches && this.touches.length == 2)
        {
            Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
        }

        this.target.transform.rotate(new Laya.Vector3(0,this.offsetXReal * this.rotaionSpeed * elapsedTime,0) ,false,false);
        this.target.transform.rotate(new Laya.Vector3(this.offsetYReal * this.rotaionSpeed * elapsedTime,0,0) ,false,false);

        this.lastMouseX = Laya.stage.mouseX;
        this.lastMouseY = Laya.stage.mouseY;
    }

    private OffsetJugd():void
    {
        if( Math.abs(this.offsetXReal) <= 0.01 )
        {
            this.offsetXReal = 0;
            this.offsetYReal = 0;
        }
    }
}